﻿using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using Component.Worker.RecursiveProcess;
using CMS.BLL;
using CMS.DAL;


public partial class AdminX_Testimonial_TestimonialMain : System.Web.UI.UserControl
{
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            this.SetLabelLanguage();
            ShowMess();
            BindData(1);
        }
    }


    private void SetLabelLanguage()
    {
        int nDefaultLanguage = Support.GetDefaultLanguage();
        btnAddnew.Text = MultipleLanguage.BTN_ADDNEW[nDefaultLanguage - 1];
    }
    #region Paging & Message

    private void ShowMess()
    {
        int nMessage = ConvertData.ConvertToInt(Request.QueryString[Config.MESS_ID]);
        int nDefaultLang = Support.GetDefaultLanguage() - 1;
        switch (nMessage)
        {
            case 1:
                MessageBoxss.Show(MessagesAlert.INSERT_SUCCESSFULL[nDefaultLang]);
                break;
            case 2:
                MessageBoxss.Show(MessagesAlert.UPDATE_SUCCESSFUL[nDefaultLang]);
                break;
            case 3:
                MessageBoxss.Show(MessagesAlert.ITEM_NOTEXIST[nDefaultLang]);
                break;
            default:

                break;
        }
    }
    #endregion

#region DataProcessing
    private void BindData(int CurrentPageIndex)
    {
        int nPageCount = 0;
        int nStatus = -1;
        int nDefLang = Support.GetDefaultLanguage();
        Testimonial objTestimonial = new Testimonial();
        DataTable dtbTestimonial = new DataTable();
        dtbTestimonial = objTestimonial.Search(0, nStatus, CurrentPageIndex, Config.PAGESIZE, ref nPageCount);
        RpTestimonial.DataSource = dtbTestimonial;
        RpTestimonial.DataBind();
        lblTotalRecord.Text = MultipleLanguage.LBL_TOTAL_ITEM[nDefLang - 1] + nPageCount + MultipleLanguage.LBL_FOUND[nDefLang - 1];


       
    }


    protected void RpTestimonial_ItemCommand(object source, RepeaterCommandEventArgs e)
    {
        int nDefaultLanguage = Support.GetDefaultLanguage() - 1;
            string strCommand = e.CommandName;
            int nID = ConvertData.ConvertToInt(e.CommandArgument);
            Testimonial objTestimonial = new Testimonial();
            int nStatus = 0;
            switch (strCommand)
            {
                case "Delete":
                    int nDelete = objTestimonial.DeleteById(nID);
                    
                    BindData(1);
                    break;

                case "Edit":
                    string sEdit = Config.ROOT + Pages.BackEnds.ADMIN + "?" + Config.PAGE + "=" + Pages.BackEnds.STR_TESTIMONIAL_ADD + "&" + Config.ACTION + "=" + Config.ACTION_EDIT + "&" + Config.ACTION_ID + "=" + nID;
                    Response.Redirect(sEdit);
                    break;

                case "Active":
                    nStatus = objTestimonial.UpdateStatus(nID, Config.STATUS_INACTIVE);

                    BindData(1);
                    break;
                case "Inactive":
                    nStatus = objTestimonial.UpdateStatus(nID, Config.STATUS_ACTIVE);
                 
                    BindData(1);
                    break;
            }
    }
    protected void  RpTestimonial_ItemDataBound(object sender, RepeaterItemEventArgs e)
    {
        int nDefaultLanguage = Support.GetDefaultLanguage() - 1;
        if ((e.Item.ItemType != ListItemType.Header) && (e.Item.ItemType != ListItemType.Footer))
        {
            Label lblFullName = (Label)e.Item.FindControl("lblFullName");
            Label lblPosition = (Label)e.Item.FindControl("lblPosition");
            Label lblContent = (Label)e.Item.FindControl("lblContent");
            LinkButton lnkActive = (LinkButton)e.Item.FindControl("lnkActive");
            LinkButton lnkInactive = (LinkButton)e.Item.FindControl("lnkInactive");
            LinkButton lnkEdit = (LinkButton)e.Item.FindControl("lnkEdit");
            LinkButton lnkDelete = (LinkButton)e.Item.FindControl("lnkDelete");

            lblFullName.Text = ConvertData.ConvertToString(DataBinder.Eval(e.Item.DataItem, "ClientName"));
            lblPosition.Text = ConvertData.ConvertToString(DataBinder.Eval(e.Item.DataItem, "ClientPosition"));
            
            
            int nStatus = ConvertData.ConvertToInt(DataBinder.Eval(e.Item.DataItem, "Status"));
            if (nStatus > 0) // Active
            {
                lnkActive.Visible = true;
                lnkInactive.Visible = false;
            }
            else
            {
                lnkActive.Visible = false;
                lnkInactive.Visible = true;
            }


            lnkEdit.CommandArgument = ConvertData.ConvertToString(DataBinder.Eval(e.Item.DataItem, "TestimonialID"));
            lnkDelete.OnClientClick = Support.CreateConfirmBoxClient(MessagesAlert.DELETE_ALERT[nDefaultLanguage]);
            lnkDelete.CommandArgument = ConvertData.ConvertToString(DataBinder.Eval(e.Item.DataItem, "TestimonialID"));
        }


        if (e.Item.ItemType != ListItemType.Footer && e.Item.ItemType != ListItemType.Header)
        {
            LinkButton lnkDelete = (LinkButton)e.Item.FindControl("lnkDelete");
            LinkButton lnkEdit = (LinkButton)e.Item.FindControl("lnkEdit");
            LinkButton lnkActive = (LinkButton)e.Item.FindControl("lnkActive");
            LinkButton lnkInactive = (LinkButton)e.Item.FindControl("lnkInactive");
            int page_ID = ConvertData.ConvertToInt(DataBinder.Eval(e.Item.DataItem, "TestimonialID"));
          
            lnkActive.Text = MultipleLanguage.STATUS_ACTIVE[nDefaultLanguage - 1];
            lnkInactive.Text = MultipleLanguage.STATUS_INACTIVE[nDefaultLanguage - 1];


            lnkEdit.CommandArgument = ConvertData.ConvertToString(DataBinder.Eval(e.Item.DataItem, "TestimonialID"));
            lnkDelete.CommandArgument = ConvertData.ConvertToString(DataBinder.Eval(e.Item.DataItem, "TestimonialID"));
            lnkDelete.OnClientClick = Support.CreateConfirmBoxClient(MessagesAlert.DELETE_CATEGORY_CONFIRM[nDefaultLanguage - 1]);

            lnkActive.CommandArgument = ConvertData.ConvertToString(DataBinder.Eval(e.Item.DataItem, "TestimonialID"));
            lnkActive.OnClientClick = Support.CreateConfirmBoxClient(MessagesAlert.INACTIVE_ALERT[nDefaultLanguage]);
            lnkInactive.CommandArgument = ConvertData.ConvertToString(DataBinder.Eval(e.Item.DataItem, "TestimonialID"));
            lnkInactive.OnClientClick = Support.CreateConfirmBoxClient(MessagesAlert.ACTIVE_ALERT[nDefaultLanguage - 1]);

        }
    }
#endregion

    protected void btnAddnew_Click1(object sender, EventArgs e)
    {
        string sAdd = Config.ROOT + Pages.BackEnds.ADMIN + "?" + Config.PAGE + "=" + Pages.BackEnds.STR_TESTIMONIAL_ADD + "&" + Config.ACTION + "=" + Config.ACTION_ADD;
        Response.Redirect(sAdd);
    }
}
    
